package com.plian.system.controller.sys.warning;

import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.sys.warning.WarningMessage;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.sys.warning.IWarningMessageService;
import com.plian.system.vo.sys.warning.WarningMessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: Take-off
 * @Description: TODO
 * @Date: 2022/12/23 11:25 AM
 * @Version: 1.0
 **/

@RestController
@AllArgsConstructor
@RequestMapping("api/warningMessage")
@Api(value = "WarningMessageController", tags = "预警消息 控制器")
public class WarningMessageController extends BaseController {

    private final IWarningMessageService warningMessageService;

    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入 warningMessage")
    public R<WarningMessageVO> detail(@RequestBody WarningMessage warningMessage) {
        return R.data(warningMessageService.detail(warningMessage));
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<WarningMessageVO> list(@RequestBody Query query) {
        return warningMessageService.list(query);
    }

    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "修改", notes = "传入 warningMessage")
    public R<WarningMessage> submit(@Valid @RequestBody WarningMessage warningMessage) {
        boolean result = warningMessageService.submit(warningMessage);
        return result ? R.data(warningMessage) : new R(CommonCode.FAIL);
    }

    @PostMapping("/feedback")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "反馈", notes = "传入 ids")
    public R feedback(@Valid @RequestBody WarningMessageVO warningMessageVO) {
        return R.status(warningMessageService.feedback(warningMessageVO));
    }

    @PostMapping("/feedback-reject")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "反馈驳回", notes = "传入 ids")
    public R feedbackReject(@Valid @RequestBody WarningMessageVO warningMessageVO) {
        return R.status(warningMessageService.feedbackReject(warningMessageVO));
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入 ids")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return warningMessageService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    @GetMapping("/sendMessage")
    @ApiOperation(value = "催办", notes = "传入 id")
    public R sendMessages(@RequestParam(name = "id",required= false)Long id) {
       warningMessageService.sendMessages(id);
        return  R.success();
    }
}
